#include "common.h"
#include "tlist.h"
#include "tbinarytree.h"
#include <stdlib.h>
#include <stdio.h>

struct test {
	struct test *next;
};

static void
log_ptr(void *p)
{
	printf("free %p\n",p);
}

static struct test *
new_test(struct test *parent)
{
	struct test *ret=(struct test*)gc_malloc(sizeof(struct test),parent,log_ptr);
	printf("new %p\n",ret);
	if (parent) {
		ret->next=parent->next;
		parent->next=ret;
	}
	else {
		ret->next=0;
	}
	return ret;
}

static void *
test(struct gc_weak_table *weak)
{
	struct test *p;
	int i;

	gc_enter();

	gc_enter();
		for (i=0;i<4;i++) {
			p=new_test(0);
			gc_link(weak,0,p);
		}

	/* after gc_leave , only last p leave in the stack */
	gc_leave(p,0);

	/* p will not be collected */
	gc_collect();

	p->next=new_test(p);

	/* one node can be linked to parent more than once */
	gc_link(p,0,p->next);

	gc_dryrun();

	gc_link(p,p->next,0);

	gc_dryrun();

	/* p will not be exist on the stack after gc_leave , it can be collected. */
	gc_leave(p->next,0);

	gc_link(weak,0,p->next);

	return p->next;
}

static void
iterate_weak_table(struct gc_weak_table *weak)
{
	int iter=0;
	void *p;
	gc_enter();
	while ((p=gc_weak_next(weak,&iter)) != 0) {
		printf("%p is alive\n",p);
	}
	gc_leave(0);
}


unsigned long HashString(char *lpszFileName, unsigned long dwHashType)
{ 
unsigned char *key = (unsigned char *)lpszFileName;
unsigned long seed1 = 0x7FED7FED, seed2 = 0xEEEEEEEE;
int ch;
while(*key != 0)
{ 
ch = toupper(*key++);
seed1 = ((dwHashType << 8) + ch) ^ (seed1 + seed2);
seed2 = ch + seed1 + seed2 + (seed2 << 5) + 3; 
}
return seed1; 
}

void printtree(int value){
	std::cout<<"value is "<<value<<"\n"<<std::endl;
}

int TestFunctions(int b)
{
	if(b==0)
		return 0;
	return TestFunctions(--b);
}

int main(void){

	int b=TestFunctions(10000);
	getchar();
	return 0;
	//unsigned long testemo=HashString("unitneutralacritter.grb",1);
	/*int value = 0 ;
	push_lstack(101);
	push_lstack(102);
	push_lstack(103);
	push_lstack(104);
	push_lstack(105);
	value=top_lstack();
	std::cout<<"top value is "<<value<<"\n"<<std::endl;
	value=pop_lstack();
	std::cout<<"pop value is "<<value<<"\n"<<std::endl;
	value=top_lstack();
	std::cout<<"top value is "<<value<<"\n"<<std::endl;
	value=pop_lstack();
	std::cout<<"pop value is "<<value<<"\n"<<std::endl;
	value=top_lstack();
	std::cout<<"top value is "<<value<<"\n"<<std::endl;
	value=pop_lstack();
	std::cout<<"pop value is "<<value<<"\n"<<std::endl;
	value=top_lstack();
	std::cout<<"top value is "<<value<<"\n"<<std::endl;
	value=pop_lstack();
	std::cout<<"pop value is "<<value<<"\n"<<std::endl;
	value=top_lstack();
	std::cout<<"top value is "<<value<<"\n"<<std::endl;
	value=pop_lstack();
	std::cout<<"pop value is "<<value<<"\n"<<std::endl;*/

	insert_tree( 1000);
	insert_tree( 2000);
	insert_tree( 2323);
	insert_tree( 123);
	insert_tree( 3213);
	insert_tree( 3211);

	int *tpoint=NULL;
	tpoint=find_tree(3213);
	std::cout<<"point is "<<*tpoint<<"\n"<<std::endl;
	tpoint=find_tree(123);
	std::cout<<"point is "<<*tpoint<<"\n"<<std::endl;
	tpoint=find_tree(2323);
	std::cout<<"point is "<<*tpoint<<"\n"<<std::endl;
	tpoint=find_tree(2000);
	std::cout<<"point is "<<*tpoint<<"\n"<<std::endl;
	tpoint=find_tree(1000);
	std::cout<<"point is "<<*tpoint<<"\n"<<std::endl;
	tpoint=find_tree(3211);
	std::cout<<"point is "<<*tpoint<<"\n"<<std::endl;
	tpoint=find_tree(10000);
	std::cout<<"point is "<<tpoint<<"\n"<<std::endl;

	std::cout<<"=============================\n"<<std::endl;
	pre_order_traverse(printtree);
	getchar();
	return 0;

	struct test *p;
	struct gc_weak_table *weak;
	gc_init();

	weak=gc_weak_table(0);
	p=(struct test*)test(weak);

	gc_enter();

	printf("%p is in weak table\n",gc_weak_next(weak,0));

	gc_leave(0);

	gc_collect();

	iterate_weak_table(weak);

	gc_dryrun();

	gc_exit();

	getchar();
	return 0;
}